package com.jyx.mail;

import java.util.Date;
import java.util.Properties;

import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart;

/**
 * javax.mail  邮件发送
 * author 		jyx
 * email 		jyxsuper@163.com
 * description: MyMail
 * Company: 	myzczk
 * datetime: 	2017年6月7日 下午4:08:09
 */

class MyMail
{
	public static void main(String[] args) throws MessagingException{
		MailSenderInfo mainInfo = new MailSenderInfo();

		mainInfo.setMailServerHost("smtp.qq.com");// 主机地址：smtp 简单邮件传输协议，默认端口25
		mainInfo.setMailServerPort("25");// 主机端口
		
		// 发送者信息
		mainInfo.setUserName("584225050");// 用户名
		mainInfo.setPassword("DENGYANG410");// 密码
		mainInfo.setFromAddress("584224040@qq.com");

		// 接收者信息
		mainInfo.setToAddress("771822044");// 接收地址
		
		mainInfo.setSubject("周六测试-----周六测试");// 标题
		mainInfo.setContent("<div>hello 这是周六mail测试用的</div>");// 邮件内容

		// 邮件发送
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendHtmlMail(mainInfo);
	}
}

/** 文件发送类 */
class  SimpleMailSender
{
	/** 发送HTML格式的邮件 param MailSenderInfo:代发送邮件信息
	 * @throws MessagingException */
	public boolean sendHtmlMail(MailSenderInfo mailInfo) throws MessagingException{
		// 判断是否需要身份验证
		MyAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();

		// 如果需要验证身份，创建一个密码验证器
		authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
		// 根据邮件会话属性和密码验证器构造一个发邮件的session
		Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
		// 根据session创建一个邮件信息
		Message mailMessage = new MimeMessage(sendMailSession);

		// 创建邮件发送者的地址
		InternetAddress from = new InternetAddress(mailInfo.getFromAddress());
		// 设置邮件消息的发送者
		mailMessage.setFrom(from);

		// 设置邮件接收者的地址
		InternetAddress to = new InternetAddress(mailInfo.getToAddress());
		// 发送邮件类型和接收者地址:把接收者地址设置成接收者
		mailMessage.setRecipient(Message.RecipientType.TO,to);

		// 设置邮件消息的主题
		mailMessage.setSubject(mailInfo.getSubject());
		// 设置邮件的发送时间
		mailMessage.setSentDate(new Date());
		
		// 创建一个容器类:包含了MimeBodyPart类型的对象
		MimeMultipart mainPart = new MimeMultipart();
		// 创建一个包含HTML内容的mimeBodyPart
		MimeBodyPart html = new MimeBodyPart();
		//
		html.setContent(mailInfo.getContent(),"text/html;charset=urt-8");
		mainPart.addBodyPart(html);

		// 发送邮件
		MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
		// 邮件发送的固定写法
		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plaini");
		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.text_html");
		mc.addMailcap("message/ref822;; x-java-content-handler=com.sun.mail.handlers.text_html");

		CommandMap.setDefaultCommandMap(mc);

		Transport.send(mailMessage);

		return true;
	}
}

/**
	验证类
*/
class MyAuthenticator extends Authenticator
{
	String userName = null;
	String password = null;

	public MyAuthenticator(){}

	public MyAuthenticator(String userName, String password){
		this.userName = userName;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthenticator(){
		return new PasswordAuthentication(userName,password);
	
	}

}


class MailSenderInfo
{
	// 发送有点的服务器的IP和端口
	private String mailServerHost;
	private String mailServerPort;

	// 发送这的地址，用户名和密码
	private String fromAddress;
	private String toAddress;
	
	private String userName;
	private String password;

	// 邮件的主题，内容
	private String subject;
	private String content;

	// 邮件的附件
	private String[] attachFileNames;

	// 获取邮件会话属性
	public Properties getProperties(){
		Properties p = new Properties();
		p.put("mail.smtp.host",this.mailServerHost);
		p.put("mail.smtp.port",this.mailServerPort);
		p.put("mail.smtp.auth","true");

		return p;
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public String getMailServerPort() {
		return mailServerPort;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String[] getAttachFileNames() {
		return attachFileNames;
	}

	public void setAttachFileNames(String[] attachFileNames) {
		this.attachFileNames = attachFileNames;
	}

	


}
